package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Common.Enum.LikedStatus;
import com.weixin.project.Common.Enum.ModuleType;
import com.weixin.project.Entity.Comment;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.CommonService;
import com.weixin.project.Service.RecommendService;
import com.weixin.project.Service.UserService;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class CommonController {
    @Autowired
    UserService userService;
    @Autowired
    CommonService commonService;
    @Autowired
    RecommendService recommendService;
    @Value("${logging.pattern.dateformat}")
    String dateFormat;
    @PostMapping("/question/comment")
    JSONObject commentQuestion(@RequestBody JSONObject params,
                               HttpServletRequest request){
        User currentUser = userService.findCurrentUser(request);
        Integer code = params.getInteger("module");
        ModuleType moduleType = ModuleType.codeOf(code);
        if (moduleType == null) {
            return ResponseUtil.error("不存在的module:" + code);
        }
        Comment comment = new Comment();
        comment.setQuestionId(params.getInteger("question_id"));
        comment.setContent(params.getString("comment_text"));
        comment.setAnswererId(currentUser.getId());
        comment.setCreateTime(new Date());
        comment.setModule(moduleType);
        commonService.storeComment(comment);
        return ResponseUtil.success();
    }

    @PostMapping("/comment/good")
    JSONObject goodQuestion(HttpServletRequest request,
                            @RequestBody JSONObject params){
        User currentUser = userService.findCurrentUser(request);
        Integer commentId = params.getInteger("comment_id");
        Comment comment = commonService.findComment(commentId);
        commonService.likeOneComment(comment, currentUser.getId());
        //TODO:recommend
//        if (comment.getModule().equals(ModuleType.QUESTION)){
//            recommendService.collectUserBehaviorForOnlineRec(currentUser.getId(), comment.getQuestionId(), RatingScore.LIKE);
//        }
        return ResponseUtil.success();
    }

    @GetMapping("/comment/get_comment")
    JSONObject getCommentsOfQuestion(HttpServletRequest request,
                                    @RequestParam("id") Integer questionId,
                                     @RequestParam("module") Integer code){
        User currentUser = userService.findCurrentUser(request);
        ModuleType moduleType = ModuleType.codeOf(code);
        List<Comment> comments = commonService.findComments(questionId, moduleType);
        JSONArray array = new JSONArray();
        for (Comment comment:comments){
            JSONObject obj = new JSONObject();
            User user = userService.findUser(comment.getAnswererId());
            obj.put("user_name", user.getName());
            obj.put("user_img", user.getImage());
            obj.put("comment_text", comment.getContent());
            obj.put("time", new SimpleDateFormat(dateFormat).format(comment.getCreateTime()));
            obj.put("good", commonService.countLikesOfComment(comment));
            obj.put("id", comment.getId());
            obj.put("has_good", LikedStatus.getBool(commonService.ifUserHasLiked(comment.getId(), currentUser.getId())));
            array.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("comments", array);
        return ResponseUtil.success("",ret);
    }

}
